package com.lovo.dao.impl;

import com.lovo.dao.BaseDAO;
import com.lovo.dao.UserDAO;
import com.lovo.pojo.UsersDO;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class UserDAOImpl extends BaseDAO implements UserDAO {
    @Override
    public UsersDO getByUsernameAndPwd(String username, String pwd) {
        this.getConnection();
        preparedStatement = null;
        rs = null;
        UsersDO users = null;
        String sql = "select * from t_users where u_name= ? and u_pwd= ?";
        try {
            preparedStatement = con.prepareStatement(sql);
            preparedStatement.setObject(1, username);
            preparedStatement.setObject(2, pwd);
            rs = preparedStatement.executeQuery();
            if (rs.next()) {
                users = new UsersDO();
                users.setId(rs.getInt("u_id"));
                users.setUserName(rs.getString("u_name"));
                users.setPwd(rs.getString("u_pwd"));
                users.setBlance(rs.getDouble("u_blance"));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            close(con, preparedStatement, rs);
        }
        return users;
    }

    @Override
    public void update(UsersDO usersDO) {
        this.getConnection();
        preparedStatement = null;
        String sql = "update t_users set u_pwd=?,u_blance=? where u_id=?";
        try {
            // 2. 获取执行SQL语句对象
            preparedStatement = con.prepareStatement(sql);
            preparedStatement.setObject(1, usersDO.getPwd());
            preparedStatement.setObject(2, usersDO.getBlance());
            preparedStatement.setObject(3, usersDO.getId());
            // 3. 执行SQL语句
            preparedStatement.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            // 4. 关闭连接
            close(con, preparedStatement);
        }
    }

    @Override
    public void update(UsersDO loginUser, UsersDO otherUser) {
        // 1. 创建连接
        Connection con = getConnection();
        PreparedStatement preparedStatement = null;
        String sql = "update t_users set u_pwd=?,u_blance=? where u_id=?";
        try {
            // 2. 获取执行SQL语句对象
            con.setAutoCommit(false);
            preparedStatement = con.prepareStatement(sql);
            preparedStatement.setObject(1, loginUser.getPwd());
            preparedStatement.setObject(2, loginUser.getBlance());
            preparedStatement.setObject(3, loginUser.getId());
            // 3. 执行SQL语句
            preparedStatement.executeUpdate();
            preparedStatement = con.prepareStatement(sql);
            preparedStatement.setObject(1, otherUser.getPwd());
            preparedStatement.setObject(2, otherUser.getBlance());
            preparedStatement.setObject(3, otherUser.getId());
            // 3. 执行SQL语句
            preparedStatement.executeUpdate();
            con.commit();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            try {
                con.rollback();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        } finally {
            // 4. 关闭连接
            close(con, preparedStatement);
        }
    }


    /*
    根据ID查询
     */
    @Override
    public UsersDO getById(int id) {
        this.getConnection();
        preparedStatement = null;
        rs = null;
        UsersDO users = null;
        String sql = "select * from t_users where u_id=?";
        try {
            preparedStatement = con.prepareStatement(sql);
            preparedStatement.setObject(1, id);
            rs = preparedStatement.executeQuery();
            if (rs.next()) {
                users = new UsersDO();
                users.setId(rs.getInt("u_id"));
                users.setUserName(rs.getString("u_name"));
                users.setPwd(rs.getString("u_pwd"));
                users.setBlance(rs.getDouble("u_blance"));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            close(con, preparedStatement, rs);
        }
        return users;
    }

    /*
    通过姓名查询
     */
    @Override
    public UsersDO getByName(String name) {
        this.getConnection();
        preparedStatement = null;
        rs = null;
        UsersDO users = null;
        String sql = "select * from t_users where u_name=?";
        try {
            preparedStatement = con.prepareStatement(sql);
            preparedStatement.setObject(1, name);
            rs = preparedStatement.executeQuery();
            if (rs.next()) {
                users = new UsersDO();
                users.setId(rs.getInt("u_id"));
                users.setUserName(rs.getString("u_name"));
                users.setPwd(rs.getString("u_pwd"));
                users.setBlance(rs.getDouble("u_blance"));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            close(con, preparedStatement, rs);
        }
        return users;
    }
}
